package com.truyen.persistence.dao.impl;

import java.util.List;

import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

import com.truyen.persistence.dao.UserLikedDao;
import com.truyen.persistence.dao.base.GenericJpaBaseDaoImpl;
import com.truyen.persistence.entity.Account;
import com.truyen.persistence.entity.TruyenTranh;
import com.truyen.persistence.entity.UserLiked;
import com.truyen.utils.PagingInfo;

@Repository
public class UserLikedDaoImpl extends GenericJpaBaseDaoImpl<UserLiked, Long>
		implements UserLikedDao {

	public UserLikedDaoImpl() {
		super(UserLiked.class);
	}

	@Override
	public List<UserLiked> getLstByAccountWithPaging(Account account,
			PagingInfo pInfo) {
		CriteriaBuilder critBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<UserLiked> query = critBuilder
				.createQuery(UserLiked.class);
		Root<UserLiked> entity = query.from(UserLiked.class);
		Predicate p = critBuilder.conjunction();
		p = critBuilder.and(p,
				critBuilder.equal(entity.<Account> get("account"), account));
		query = query.where(p);
		query.orderBy(critBuilder.desc(entity.get("id")));
		TypedQuery<UserLiked> q = entityManager.createQuery(query);
		long count = countByQuery(query, entity);
		pInfo.setCount(count);
		pInfo.setTotalPage((int)Math.ceil((double)count/pInfo.getSizePerPage()));		
		q.setFirstResult(pInfo.getStartResult());
		q.setMaxResults(pInfo.getSizePerPage());
		List<UserLiked> lst = q.getResultList();
		return (lst.size() > 0) ? lst : null;
	}

	@Override
	public UserLiked findByAccount(Account account, String nameTruyen) {
		CriteriaBuilder critBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<UserLiked> query = critBuilder
				.createQuery(UserLiked.class);
		Root<UserLiked> entity = query.from(UserLiked.class);
		Predicate p = critBuilder.conjunction();
		p = critBuilder.and(p,
				critBuilder.equal(entity.<Account> get("account"), account),
				critBuilder.equal(entity.<String> get("nameTruyen"), nameTruyen));
		query = query.where(p);
		TypedQuery<UserLiked> q = entityManager.createQuery(query);
		q.setFirstResult(0);
		q.setMaxResults(1);
		List<UserLiked> lst = q.getResultList();
		return (lst.size() > 0) ? lst.get(0) : null;
	}

//	@Override
//	public UserLiked findByAccountTruyenTranh(Account account,
//			TruyenTranh truyenTranh) {
//		CriteriaBuilder critBuilder = entityManager.getCriteriaBuilder();
//		CriteriaQuery<UserLiked> query = critBuilder
//				.createQuery(UserLiked.class);
//		Root<UserLiked> entity = query.from(UserLiked.class);
//		Predicate p = critBuilder.conjunction();
//		p = critBuilder.and(p,
//				critBuilder.equal(entity.<Account> get("account"), account),
//				critBuilder.equal(entity.<TruyenTranh> get("truyenTranh"), account));
//		query = query.where(p);
//		TypedQuery<UserLiked> q = entityManager.createQuery(query);
//		q.setFirstResult(0);
//		q.setMaxResults(1);
//		List<UserLiked> lst = q.getResultList();
//		return (lst.size() > 0) ? lst.get(0) : null;
//	}

}
